package com.ai3d.rfid.common.libs

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.ai3d.rfid.base.app.App
import com.ai3d.rfid.common.log.Logcat

/**
 * @ProjectName : Trias
 * @Author : weijinsong
 * @Time : 2022/10/19 16:36
 * @Description : 配置抽象类
 */
abstract class ActivityLifecycle {
    var activityList: ArrayList<Activity> = ArrayList()
    private var activityCount = 0
    open fun init(app: App) {
        if (activityList == null) {
            activityList = ArrayList()
        }
        app.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, p1: Bundle?) {
                this@ActivityLifecycle.onActivityCreated(activity, p1)
                activityList.add(activity)
            }

            override fun onActivityStarted(p0: Activity) {
                if (activityCount == 0) {
                    Logcat.d("ApplicationLifecycleObservable  -->  Lifecycle >>>>>>>>>>>>>>>>>>> APP  Foreground");
                }
                activityCount++
            }

            override fun onActivityResumed(p0: Activity) {

            }

            override fun onActivityPaused(p0: Activity) {

            }

            override fun onActivityStopped(p0: Activity) {
                activityCount--
                if (activityCount == 0) {
                    Logcat.d("ApplicationLifecycleObservable  -->  >>>>>>>>>>>>>>>>>>> APP  Background ");
                }
            }

            override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {

            }

            override fun onActivityDestroyed(p0: Activity) {
                if (activityList != null) {
                    activityList!!.remove(p0)
                }
            }

        })
    }

    open fun onActivityCreated(activity: Activity, p1: Bundle?) {

    }
}